// pages/view/ip/DrugRevenueRatio/DrugRevenueRatio.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var chartData = {
  main: {
    title: '住院科室药品收入比例',
    drug_data: [15, 20, 45, 37],
    med_data: [20, 18, 55, 35],
    categories: ['2012', '2013', '2014', '2015']
  }
};
Page({
  data: {
    chartTitle: '住院科室药品收入比例',
    deptFee_list:[
    ],
    sum_list:{
      totalFee: 0,
      drugFee: 0,
      drugInIP: ''
    }
   
  },
  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)
   
  },

  /**
   * 调用封装好的网络请求，获取数据
   */
  myRequest: function (options) {
    var that = this
    console.log('options:', options)
    //调用封装好的网络请求，获取数据//options.startTime + '&endTime=' + options.endTime
    network.get('/ip/drug?startTime=' + '2018-01-01' + '&endTime=' + '2018-01-30', {})
      .then(function (res) {
        console.log('res:', res)
        var obj1 = res.data.data.list1
        var obj2 = res.data.data.list2
        chartData.main.categories = res.data.data_2.list1
        chartData.main.drug_data = res.data.data_2.list2
        chartData.main.med_data = res.data.data_2.list3
        console.log('obj2:', obj2)
        that.setData({
          deptFee_list: obj1,
          sum_list: obj2

        })
        console.log('deptFee_list:', that.data.deptFee_list)
        console.log('categories:', chartData.main.categories)
        console.log('drug_data:', chartData.main.drug_data)

      }).then(function (res) {
        wx.hideLoading()
        that.createColumChart();

      }).catch(function (res) {
        console.log(res)
      })
  },
  createColumChart: function(){
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: chartData.main.categories,
      series: [{
        name: '药品金额',
        data: chartData.main.drug_data,
        format: function (val, name) {
          return val.toFixed(2) + '元';
        }
      },
      {
        name: '医疗金额',
        data: chartData.main.med_data,
        format: function (val, name) {
          return val.toFixed(2) + '元';
        }
      },

      ],
      yAxis: {
        format: function (val) {
          return val + '元';
        },
        title: '金额',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  }
});